using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_YesNoVote : BaseTemplate
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            flashHeader.Variables.Add("SiteRoot", this.GetSiteRoot());
            SetAverage();
            if(User != null && User.Identity != null && User.Identity.IsAuthenticated)
            {
                Acms.Bll.UserPostVote tmp = Acms.Bll.UserPostVote.GetByUserIdAndPostId(User.Identity.User.Id, PostId);
                hasVoted = tmp != null;
                SetPrevVote(tmp);
            }
        }
    }

    protected void lstComments_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        Acms.Bll.UserPostComment comment = e.Item.DataItem as Acms.Bll.UserPostComment;
        DataBindComment(comment, e.Item);
    }

    private void DataBindComment(Acms.Bll.UserPostComment comment, Acms.WebControls.AcmsDataListItem item)
    {
        if (comment != null && item != null)
        {
            PlaceHolder phNormal = item.FindControl("phNormal") as PlaceHolder;
            PlaceHolder phReported = item.FindControl("phReported") as PlaceHolder;
            PlaceHolder phSuspended = item.FindControl("phSuspended") as PlaceHolder;
            LinkButton lnkReport = item.FindControl("lnkReport") as LinkButton;

            if (phNormal != null && phReported != null && phSuspended != null)
            {
                phNormal.Visible = false;
                phReported.Visible = false;
                phSuspended.Visible = false;

                if (comment.Suspended)
                {
                    phSuspended.Visible = true;
                }
                else if (comment.Reported)
                {
                    phReported.Visible = true;
                }
                else
                {
                    phNormal.Visible = true;
                    lnkReport.Visible = !comment.Reviewed;
                }
            }
        }
    }

    protected void lstComments_ItemCommand(object sender, Acms.WebControls.AcmsDataListItemCommandEventArgs e)
    {
        if (e.CommandName == "Report")
        {
            int id = (int)lstComments.DataKeys[e.Item.DisplayIndex]["Id"];
            object concurencyId = (object)lstComments.DataKeys[e.Item.DisplayIndex]["ConcurencyId"];
            Acms.Bll.UserPostComment comment = Acms.Bll.UserPostComment.GetById(id);
            if (comment != null)
            {
                comment.CheckConcurent(concurencyId);
                comment.Reported = true;
                comment.Update();
                DataBindComment(comment, e.Item);
            }
        }
        else if (e.CommandName == "Show")
        {
            PlaceHolder phNormal = e.Item.FindControl("phNormal") as PlaceHolder;
            PlaceHolder phReported = e.Item.FindControl("phReported") as PlaceHolder;
            if (phNormal != null && phReported != null)
            {
                phReported.Visible = false;
                phNormal.Visible = true;
            }
        }
    }

    protected void lstComments_DataBound(object sender, EventArgs e)
    {
        lblTotalComments.Text = lstComments.Items.Length.ToString();    
    }


    protected void btnAddComment_Click(object sender, EventArgs e)
    {
        if (IsValid && User != null && User.Identity != null && User.Identity.IsAuthenticated)
        {
            TextBox txtTitle = loginView.FindControl("txtTitle") as TextBox;
            TextBox txtComment = loginView.FindControl("txtComment") as TextBox;

            if (txtTitle != null && txtComment != null)
            {
                Acms.Bll.UserPostComment comment = Acms.Bll.UserPostComment.CreateInstance(User.Identity.User.Id, PostId, System.Net.IPAddress.Parse(Request.UserHostAddress));
                comment.Title = txtTitle.Text;
                comment.Comment = txtComment.Text;
                comment.Update();
                txtTitle.Text = txtComment.Text = null;
                lstComments.DataBind();
                pnlComments.Update();
            }
        }
    }
    
    protected void dsUserComments_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UserPostComment> e)
    {
        e.SetData(Acms.Bll.UserPostComment.GetAllByPostId(PostId));
    }

    protected void btnYes_Click(object sender, EventArgs e)
    {
        SetVote(1);
        SetAverage();
        pnlGraph.Update();
    }

    protected void btnNo_Click(object sender, EventArgs e)
    {
        SetVote(0);
        SetAverage();
        pnlGraph.Update();
    }

    private void SetVote(int value)
    {
        Acms.Bll.UserPostVote tmp = null;
        if (hasVoted)
        {
            tmp = Acms.Bll.UserPostVote.GetByUserIdAndPostId(User.Identity.User.Id, PostId);
        }
        else
        {
            tmp = Acms.Bll.UserPostVote.CreateInstance(User.Identity.User.Id, PostId);
        }
        tmp.Score = value;
        tmp.Update();
        hasVoted = true;
        SetPrevVote(tmp);
    }

    private void SetAverage()
    {
        Acms.Bll.UserPostVoteAverage ave = Acms.Bll.UserPostVoteAverage.GetByPostId(PostId);
        if (ave == null)
        {
            flashGraph.Variables["valueOne"] = (0.5).ToString();
        }
        else
        {
            flashGraph.Variables["valueOne"] = ave.Average.ToString();
        }
    }

    private void SetPrevVote(Acms.Bll.UserPostVote prev)
    {
        Panel pnlPrevVote = loginView.FindControl("pnlPrevVote") as Panel;
        Label lblPrevVote = loginView.FindControl("lblPrevVote") as Label;

        if (pnlPrevVote != null && lblPrevVote != null)
        {
            if (prev != null)
            {
                pnlPrevVote.Visible = true;
                lblPrevVote.Text = (prev.Score == 1) ? "Sienna" : "Kelly";
            }
            else
            {
                pnlPrevVote.Visible = false;
            }
        }
    }

    bool hasVoted = false;

    protected override object SaveViewState()
    {
        ViewState["hasVoted"] = hasVoted;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["hasVoted"] is bool)
            hasVoted = (bool)ViewState["hasVoted"];
    }

    protected string GetLoginUrl()
    {
        return ResolveClientUrl(string.Concat("~/Login.aspx?ReturnUrl=", CurrentUrl.Replace("~", Request.ApplicationPath).Replace("//", "/")));
    }

    protected string GetLoginImageUrl()
    {
        return ResolveClientUrl("~/img/needToLogin.gif");
    }
}
